<template>
  <el-container>
    <el-aside width="auto">
      <div :class="['logo-container', isMenuCollapse ? 'logo-collapse-container' : '']">
        <img src="@/assets/logo_text_white.png" class="logo" />
      </div>
      <el-menu
        background-color="#001529"
        text-color="#ffffff"
        :collapse="isMenuCollapse"
        router
        :default-active="$route.path"
      >
        <template v-for="menu in menuList">
          <el-submenu v-if="menu.children" :index="menu.index" :key="menu.index">
            <template slot="title">
              <i :class="`iconfont ${menu.icon}`"></i>
              <span>{{menu.title}}</span>
            </template>
            <el-menu-item
              v-for="subMenu in menu.children"
              :index="subMenu.index"
              :key="subMenu.index"
            >
              <i :class="`iconfont ${subMenu.icon}`"></i>
              <span slot="title">{{subMenu.title}}</span>
            </el-menu-item>
          </el-submenu>
          <el-menu-item v-else :index="menu.index" :key="menu.index">
            <i :class="`iconfont ${menu.icon}`"></i>
            <span>{{menu.title}}</span>
          </el-menu-item>
        </template>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header height="64px">
        <i :class="isMenuCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'" @click="collapseMenu"></i>
        <el-dropdown @command="command">
          <div class="account">
            <i class="iconfont icon-avatar"></i>
            <div class="nickname">{{nickname}}</div>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item icon="el-icon-setting" command="setting">个人设置</el-dropdown-item>
            <el-dropdown-item icon="el-icon-switch-button" divided command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <el-main>
        <div class="breadcrumb-container" v-show="$route.meta.breadcrumb">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item
              :to="item.route ? { name: item.route } : {}"
              v-for="(item, index) in $route.meta.breadcrumb"
              :key="index"
            >{{item.name}}</el-breadcrumb-item>
          </el-breadcrumb>
          <div
            class="title"
          >{{$route.meta.breadcrumb && $route.meta.breadcrumb[$route.meta.breadcrumb.length - 1].name}}</div>
        </div>
        <keep-alive>
          <router-view></router-view>
        </keep-alive>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import { authService, menuService } from '@/services';
import { primaryColor } from '@/styles/variables.scss';

export default {
  name: 'Dashboard',
  data() {
    return {
      primaryColor,
      isMenuCollapse: false,
      menuList: [],
    };
  },
  computed: mapState({
    nickname: (state) => state.loginSysUserVo && state.loginSysUserVo.nickname,
    loginSysUserVo: (state) => state.loginSysUserVo,
  }),
  created() {
    const loginSysUserVo = JSON.parse(localStorage.getItem('loginSysUserVo'));
    const permissionCodes = JSON.parse(localStorage.getItem('permissionCodes'));
    if (loginSysUserVo && permissionCodes) {
      this.menuList = menuService.getMenuList(permissionCodes);
    }
  },
  methods: {
    ...mapActions(['updateDeviceTypeList']),
    collapseMenu() {
      this.isMenuCollapse = !this.isMenuCollapse;
    },
    command(c) {
      if (c === 'logout') {
        this.logout();
      } else {
        this.$router.push({ name: c });
      }
    },
    async logout() {
      try {
        await this.$confirm('是否确认退出登录?', '退出登录', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        });
        authService.logout().then(() => {
          this.$router.push({ name: 'login' });
        });
      } catch (e) {
        console.error(e);
      }
    },
  },
  watch: {
    'loginSysUserVo': {
      handler: function() {
        if (this.loginSysUserVo) {
          const permissionCodes = JSON.parse(localStorage.getItem('permissionCodes'));
          this.menuList = menuService.getMenuList(permissionCodes);
        }
      },
      deep: true,
    },
  },
};
</script>

<style lang="scss" scoped>
.el-container {
  height: 100vh;
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
    z-index: 9;
    .el-icon-s-fold,
    .el-icon-s-unfold {
      font-size: 20px;
      cursor: pointer;
      color: $font-secondary-color;
    }
    .account {
      display: flex;
      align-items: center;
      cursor: pointer;
      .icon-avatar {
        font-size: 24px;
        color: $primary-color;
      }
      .nickname {
        font-size: 15px;
        color: $font-secondary-color;
      }
    }
  }
  .el-aside {
    background-color: #001529;
    box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
    z-index: 10;
    .logo-container {
      width: 250px;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 85px;
      .logo {
        height: 48px;
      }
      .logo-text {
        height: 36px;
        margin-left: 12px;
      }
    }
    .logo-collapse-container {
      width: 64px;
    }
    .el-menu {
      border-right: none;
    }
    .el-menu:not(.el-menu--collapse) {
      width: 250px;
    }
    .el-menu-item.is-active {
      color: #ffffff;
      background-color: $primary-color !important;
    }
  }
  .el-main {
    background-color: $background-color;
    padding: 20px;
    .breadcrumb-container {
      background-color: #ffffff;
      padding: 16px 24px 12px;
      .title {
        font-size: 20px;
        font-weight: 600;
        line-height: 32px;
        color: $font-primary-color;
        margin-top: 6px;
      }
    }
  }
}
</style>
